﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Common
{
    public class ToolsFile
    {
        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileContent"></param>
        public static void CreateFile(string filePath, string fileContent,Encoding encode)
        {
            FileInfo fileInfo = new FileInfo(filePath);
            if (!Directory.Exists(fileInfo.DirectoryName))
                Directory.CreateDirectory(fileInfo.DirectoryName);

            //创建文件流
            FileStream fs = new FileStream(filePath, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs, encode);
            sw.Write(fileContent);
            sw.Close();
            fs.Close();
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetFileText(string path, Encoding code)
        {
            string result = "";
            try
            {
                using (StreamReader sr = new StreamReader(path, code))
                {
                    result = sr.ReadToEnd();
                }
            }
            catch { }
            return result;
        }
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetFileText(string path)
        {
            string result = "";
            try
            {
                using (StreamReader sr = new StreamReader(path,Encoding.UTF8))
                {
                    result = sr.ReadToEnd();
                }
            }
            catch { }
            return result;
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<string> GetFileContent(string path)
        {
            List<string> result = new List<string>();
            try
            {
                using (StreamReader sr = new StreamReader(path))
                {
                    String line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        result.Add(line);
                    }
                }
            }
            catch { }
            return result;
        }

        /// <summary>
        /// 文件清零
        /// </summary>
        /// <param name="path"></param>
        public static void TruncFile(string path)
        {
            if (File.Exists(path))
            {
                FileStream fs = new FileStream(path, FileMode.Truncate);
                fs.Flush();
                fs.Close();
            }
        }

        /// <summary>
        /// 处理后面换行
        /// </summary>
        /// <param name="str"></param>
        /// <param name="strOld"></param>
        /// <param name="strNew"></param>
        /// <returns></returns>
        public static string ReplaceAndRemoveAfterEnter(string str, string strOld, string strNew)
        {
            if (str.IndexOf(strOld + "\r\n") >= 0)
                str = str.Replace(strOld + "\r\n", strNew);
            else if (str.IndexOf(strOld + "\n") >= 0)
                str = str.Replace(strOld + "\n", strNew);
            else
                str = str.Replace(strOld, strNew);
            return str;
        }



        /// <summary>
        /// 处理后面换行
        /// </summary>
        /// <param name="str"></param>
        /// <param name="strOld"></param>
        /// <param name="strNew"></param>
        /// <returns></returns>
        public static string RemoveAfterEnter(string str)
        {
            str = RemoveAfterBlank(str);
            if (str.EndsWith("\r\n"))
                str = str.Substring(0, str.Length - 2);
            else if (str.EndsWith("\n"))
                str = str.Substring(0, str.Length - 1);
            return str;
        }


        /// <summary>
        /// 处理前面换行
        /// </summary>
        /// <param name="str"></param>
        /// <param name="strOld"></param>
        /// <param name="strNew"></param>
        /// <returns></returns>
        public static string RemoveBeforeEnter(string str)
        {
            str = RemoveBeforeBlank(str);
            if (str.StartsWith("\r\n"))
                str = str.Substring(2, str.Length - 2);
            else if (str.StartsWith("\n"))
                str = str.Substring(1, str.Length - 1);
            return str;
        }

        public static string RemoveBeforeBlank(string str)
        {
            if (str.IndexOf(' ')==0)
            {
                str = str.Substring(1, str.Length - 1);
                return RemoveBeforeBlank(str);
            }
            else
                return str;
        }

        public static string RemoveAfterBlank(string str)
        {
            if (str.IndexOf(' ') == str.Length -1)
            {
                str = str.Substring(0, str.Length - 1);
                return RemoveAfterBlank(str);
            }
            else
                return str;
        }

        /// <summary>
        /// 遍历文件夹及目录，完成模板拷贝
        /// </summary>
        /// <param name="dirPath"></param>
        public static List<string> CopyDirectory(string oldPath, string newPath, string dirPath, List<string> dirExcept, List<string> fileExcept)
        {
            List<string> allFiles = new List<string>();
            DirectoryInfo directory = new DirectoryInfo(dirPath);
            FileInfo[] files = directory.GetFiles();
            foreach (FileInfo fi in files)
            {
                if (fileExcept != null)
                {
                    bool isExcepted = false;
                    foreach (string str in fileExcept)
                    {
                        if (str == fi.Name)
                        {
                            isExcepted = true;
                            break;
                        }
                    }
                    if (isExcepted)
                        continue;
                }

                fi.CopyTo(fi.FullName.Replace(oldPath, newPath), true);
                allFiles.Add(fi.FullName.Replace(oldPath, newPath));
            }

            DirectoryInfo[] dirs = directory.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {
                if (dirExcept != null)
                {
                    bool isExcepted = false;
                    foreach (string str in dirExcept)
                    {
                        if (str == dir.Name)
                        {
                            isExcepted = true;
                            break;
                        }
                    }
                    if (isExcepted)
                        continue;
                }

                Directory.CreateDirectory(dir.FullName.Replace(oldPath, newPath));
                List<string> fi = CopyDirectory(oldPath, newPath, dir.FullName, dirExcept, fileExcept);
                if (fi != null)
                {
                    foreach (string s in fi)
                    {
                        allFiles.Add(s);
                    }
                }
            }
            return allFiles;
        }
    }
}
